package com.eigaland.change_machine.business.movielist

import androidx.lifecycle.MutableLiveData
import com.eigaland.change_machine.displaymodel.MovieListItemDisplayModel
import com.eigland.eigaland_lib.base.BaseVM
import com.eigland.eigaland_lib.model.TicketRecordItem
import com.orhanobut.logger.Logger
import movie6.posprintercept2.extensions.customLaunch

public open class MovieListFragmentVM :BaseVM() {

    var movieListDataVMS = MutableLiveData<MovieListItemDisplayModel>()

    var showTakeTicketDialogStatus = MutableLiveData<Boolean>()

    var goToMovieListFullPageStatus = MutableLiveData<Boolean>()

    var repository = MovieListFragmentRepository()

    var takeTicketDialogSuccess = MutableLiveData<List<TicketRecordItem>>()

    var dialogErrorMessage = MutableLiveData<String>()


    override fun initData() {
        progressLoadingStatus.postValue(true)

        movieListDataVMS
        customLaunch({
           var result = repository.getMovieList()
            movieListDataVMS.postValue(result!!)
            progressLoadingStatus.postValue(false)
        },{
            it.printStackTrace()
            Logger.e("error")
        })
    }

    fun takeTicket(orderNo:String){

        customLaunch({
            var displayModel = repository.takeTicket(orderNo)
            if(displayModel?.errorMsg.isNotEmpty()){
                dialogErrorMessage.postValue(displayModel.errorMsg)
            } else {
                takeTicketDialogSuccess.postValue(displayModel.ticketList)
            }

        },{},{})


    }

    override fun destroy() {

    }

    override fun saveData() {
    }

    override fun onResume() {

    }


    fun buyTicket(){
        goToMovieListFullPageStatus.postValue(true)
    }

    fun takeTicket(){
        showTakeTicketDialogStatus.postValue(true)
    }


}